package net.huashitong.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Date;

/**
 * 〈服务号工具类〉
 *
 * @author shaoxian.zhong
 * @create 2018/5/12
 */
public class   WeChatUtil {

    public static final String TOKEN = "shenluntest20180512";
//    public static final String APPID = "wx97360831f9dec45f";
     public static final String APPID = "wx25e329224e4ecde3";
//    public static final String APPSECRET = "d128b8378b6c860460693922c0003631";
    public static final String APPSECRET = "09b5c80aaf531a770a28cea248de86f7";


    public static String accessToken; //调用各接口的凭证
    public static long expiresTime; //凭证失效时间

    //获取凭证
    public static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + APPSECRET;
    //创建菜单
    public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + getAccessToken();
    //查询菜单
    public static final String GET_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + getAccessToken();
    //删除菜单
    public static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + getAccessToken();

    /**
     * 创建自定义菜单
     */
    public static void createMenu(String meunStr) {
        String result = HttpUtils.doPost(CREATE_MENU_URL, meunStr);
        System.out.println(result);

    }

    /**
     * 获取accessToken
     */
    public static String getAccessToken() {
        if (accessToken == null || new Date().getTime() > expiresTime) {
            String jsonStr = getJSONStr(GET_ACCESS_TOKEN_URL);
            JSONObject object = JSONObject.parseObject(jsonStr);
            System.out.println(object);
            accessToken = object.getString("access_token");
            Long expires_in = object.getLong("expires_in");
            expiresTime = new Date().getTime() + ((expires_in - 300) * 1000);
        }
        return accessToken;
    }

    /**
     * 查询菜单
     */
    public static String getMenuInfo() {
        return getJSONStr(GET_MENU_URL);
    }

    /**
     * 删除自定义菜单
     */
    public static String getDelMenuInfo() {
        JSONObject object = JSONObject.parseObject(getJSONStr(DELETE_MENU_URL));
        return object.getString("errmsg");
    }

    /**
     * 通过get方式获取json数据
     *
     * @param url
     * @return
     */
    private static String getJSONStr(String url) {
        try {
            return HttpUtils.doGet(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String createQcode() {

        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("expire_seconds",604800);
        jsonObject.put("action_name", "QR_LIMIT_SCENE");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("scene_id", 123);

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("scene", jsonObject1);

        jsonObject.put("action_info", jsonObject2);
        System.out.println(jsonObject);

        String str = "{\"action_name\":\"QR_LIMIT_SCENE\",\"action_info\":{\"scene\":{\"scene_id\":1}}}";
        String result = HttpUtils.doPost("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + getAccessToken(), str);
        System.out.println(result);
        //https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQH28DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZ3A0b2hvRVFkS2kxMDAwME0wM3kAAgSnHP1aAwQAAAAA
        return "";
    }

    /**
     * 获取素材列表
     *
     * @return
     */
    public static String batchgetMaterial() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "image");
        jsonObject.put("offset", 0);
        jsonObject.put("count", 1);

        return HttpUtils.doPost("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + getAccessToken(), jsonObject.toJSONString());
    }

    /**
     * 客服发送消息-图片
     *
     * @return
     */
    public static String sendKfImage() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", "oKHRu0ollnkQpj8wCwRVf4NtFzz4");
        jsonObject.put("msgtype", "image");

        JSONObject jsonObject1 = new JSONObject();
        String result = batchgetMaterial();

        JSONObject resultJson = JSONObject.parseObject(result);

        jsonObject1.put("media_id", JSONObject.parseObject(JSONObject.parseArray(resultJson.get("item").toString()).get(0).toString()).get("media_id"));
        jsonObject.put("image", jsonObject1);

        return HttpUtils.doPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + getAccessToken(), jsonObject.toJSONString());
    }

    /**
     * 客服发送消息-文字
     *
     * @return
     */
    public static String sendKfMsg(String touser) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", touser);
        jsonObject.put("msgtype", "text");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("content", "课程获取方式如下：\n" +
                " \n" +
                "1、将下方弹出的海报，保存到手机后，转发至朋友圈，请至少保留两个小时以上，切记不要分组哦\n" +
                "2、将转发截图发回这里，即可得到免费领取链接\n" +
                " \n" +
                "感谢你的认可，我们相信，好的课程是值得你与朋友分享，祝你早日上岸！\n" +
                " ");
        jsonObject.put("text", jsonObject1);
        System.out.println(jsonObject);
        return HttpUtils.doPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + getAccessToken(), jsonObject.toJSONString());
    }

    /**
     * 获取用户信息
     */
    public static String getUserInfo(String accessToken, String openId) {

        return HttpUtils.doGet("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN");
    }

    /**
     * 发送模板消息-领取优惠券成功通知
     *
     * @param
     */
    public static String sendGetCouponTemplateMsg(String userOpenid) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", userOpenid);
        jsonObject.put("template_id", "");
        jsonObject.put("url", "http://www.shenlun.com/course/getCourseInfo/20180517100000000035");
        jsonObject.put("data", getCouponTemplateMsg());

        return HttpUtils.doPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + getAccessToken(), jsonObject.toJSONString());
    }

    public static JSONObject getCouponTemplateMsg() {
        JSONObject jsonData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", "恭喜您获得1张申论一点通优惠券！");
        first.put("color", "#173177");
        jsonData.put("first", first);

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", "200元");
        keyword1.put("color", "#173177");
        jsonData.put("first", keyword1);

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", "课程");
        keyword2.put("color", "#173177");
        jsonData.put("keyword2", keyword2);

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", "满1999元可用");
        keyword3.put("color", "#173177");
        jsonData.put("keyword3", keyword3);

        JSONObject keyword4 = new JSONObject();
        keyword4.put("value", "2018-05-25");
        keyword4.put("color", "#173177");
        jsonData.put("keyword4", keyword4);

        JSONObject remark = new JSONObject();
        remark.put("value", "快去使用吧");
        remark.put("color", "#173177");
        jsonData.put("remark", remark);

        return jsonData;
    }


    public static String getMenuJson() {
        //优惠活动
        JSONObject subBtn1 = new JSONObject();
        subBtn1.put("type", "view");
        subBtn1.put("name", "领取优惠券");
        subBtn1.put("url", "http://www.shenlun.com/mobile/cn/activity/coupon.html?aid=20180516100000000003");

        JSONObject subBtn2 = new JSONObject();
        subBtn2.put("type", "click");
        subBtn2.put("name", "领取《申论基础课程》");
        subBtn2.put("key", "basic_course");

        JSONObject subBtn = new JSONObject();
        JSONArray array = new JSONArray();
        array.add(subBtn1);
        array.add(subBtn2);

        subBtn.put("name", "时政热点");
        subBtn.put("sub_button", array);

//            //课程
        JSONObject subBtn1c = new JSONObject();
        subBtn1c.put("type", "view");
        subBtn1c.put("name", "《申论全程班》");
        subBtn1c.put("url", "http://8admem.natappfree.cc/course/getCourseInfo/20180205100000000017");

        JSONObject subBtn2c = new JSONObject();
        subBtn2c.put("type", "view");
        subBtn2c.put("name", "《申论基础课程》");
        subBtn2c.put("url", "http://www.shenlun.com/course/getCourseInfo/20180521100000000036");

        JSONObject subBtn3c = new JSONObject();
        subBtn3c.put("type", "view");
        subBtn3c.put("name", "《申论系统课程》");
        subBtn3c.put("url", "http://www.shenlun.com/course/getCourseInfo/20171103100000000020");

        JSONObject subBtn4c = new JSONObject();
        subBtn4c.put("type", "view");
        subBtn4c.put("name", "《申论提升训练营》");
        subBtn4c.put("url", "http://www.shenlun.com/course/getCourseInfo/20180522100000000037");

        JSONObject subBtn5c = new JSONObject();
        subBtn5c.put("type", "view");
        subBtn5c.put("name", "《申论38篇》");
        subBtn5c.put("url", "http://www.shenlun.com/course/getCourseInfo/20180124100000000026");
//            subBtn5c.put("url", "http://shenlun-test.mynatapp.cc/course/getCourseInfo/20180205100000000016");

        JSONObject subBtnc = new JSONObject();
        JSONArray arrayc = new JSONArray();
        arrayc.add(subBtn1c);
        arrayc.add(subBtn2c);
        arrayc.add(subBtn3c);
        arrayc.add(subBtn4c);
        arrayc.add(subBtn5c);

        subBtnc.put("name", "押题包");
        subBtnc.put("sub_button", arrayc);

//            //个人中心
        JSONObject subBtn2My = new JSONObject();
        subBtn2My.put("type", "view");
        subBtn2My.put("name", "我的课程");
        subBtn2My.put("url", "http://www.shenlun.com/doSign/person/myaccount?tab=course");

        JSONObject subBtn3My = new JSONObject();
        subBtn3My.put("type", "view");
        subBtn3My.put("name", "我的资料11");
        subBtn3My.put("url", "http://8admem.natappfree.cc/doSign/person/myaccount?tab=person");

        JSONObject subBtn4My = new JSONObject();
        subBtn4My.put("type", "view");
        subBtn4My.put("name", "关于我们");
        subBtn4My.put("url", "http://www.shenlun.com/about/about");

        JSONObject subBtn5My = new JSONObject();
        subBtn5My.put("type", "view");
        subBtn5My.put("name", "下载app");
        subBtn5My.put("url", "http://www.shenlun.com/appPage");

        JSONObject subBtncMy = new JSONObject();
        JSONArray arraycMy = new JSONArray();
        arraycMy.add(subBtn2My);
        arraycMy.add(subBtn3My);
        arraycMy.add(subBtn4My);
        arraycMy.add(subBtn5My);

        subBtncMy.put("name", "开通VIP");
        subBtncMy.put("sub_button", arraycMy);

        JSONObject btn = new JSONObject();

        JSONArray arrayBtn = new JSONArray();
        arrayBtn.add(subBtn);
        arrayBtn.add(subBtnc);
        arrayBtn.add(subBtncMy);

        btn.put("button", arrayBtn);
        return btn.toJSONString();
    }

    public static void main(String[] args) {
//        createQcode();
        try {
            System.out.println(getMenuJson());
            createMenu(getMenuJson());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
